<?php

namespace App\Repository\Remark;

use App\Models\Remark\DouYinRemarkModel as model;
use App\Repository\BaseRepository;
use Illuminate\Support\Facades\DB;

class DouYinRemarkRepository extends  BaseRepository
{
  public function __construct(model $model)
  {
    $this->model = $model;
    parent::__construct();
  }
  public function index($request)
  {
    $where['like'] = ['name', 'phone', 'company_name'];
    $cron['like'] = $this->getWhere($where['like'], $request);
    if (!empty($request['created_start']) && !empty($request['created_end'])) {
      $cron['between']['create_time'][] = $request['created_start'];
      $cron['between']['create_time'][] = $request['created_end'];
    }

    $cron['order']['orderby'] = isset($request['orderby']) ? $request['orderby'] : 'id';
    $cron['order']['orderdir'] = isset($request['orderdir']) ? $request['orderdir'] : 'desc';
    $pageNums = 0;
    $cron['limit']['number'] = isset($request['pagenums']) ? ($request['page_size']) : 10;
    if (isset($request['page']) && !empty(($request['page']))) {
      $pageNums = ($request['page'] - 1) * $cron['limit']['number'];
    }
    $cron['limit']['start'] = $pageNums;
    $cron['where']['douyin_remark.status'] = 0;
    $cron['join'][] = ['table' => 'douyin_catch', 'left' => 'dist_orderno', 'right' => ('douyin_remark.order_num')];
    $cron['select'] = [('douyin_catch.*'), 'douyin_remark.remark as remarkss', 'douyin_remark.id as remarkId'];
    $cron['where']['douyin_remark.user_id'] = $request['user']['id'];
    if (!empty($request->get('params'))) {
      $time = $request->get('params');
      $where['between'] = ['douyin_remark.created_at' => [$time['beginTime'], $time['endTime']]];
    }
    return $this->search($this->model, $cron, 1, 10);
  }
  //添加
  public function add($data)
  {
    //必填 字段
    $mustField = [
      'order_num',
      'user_id'
    ];
    $optionalFields = $this->model->getTableField();
    $msgField = [
      'order_num' => '订单编号不能为空',
      'user_id' => '指定员工不能为空'
    ];
    $insertData  = $this->getRealData($data, $mustField, $optionalFields, $msgField);
    if (!empty($insertData) && is_string($insertData)) {
      //返回错误参数
      return $this->returnMsg(
        2,
        $insertData
      );
    }
    // 判断是否存在 如果存在 则将其修改成流转
    // $this->model->where(['id'=>''])->update();
    $userArr = DB::table('admin_users')->where(['id' => $insertData['user_id']])->first();
    if (!$userArr) {
      return $this->returnMsg(100, '未查询到员工信息');
    }
    $insertData['user_name'] = $userArr->user_name;
    $insertData['shop_id'] = $data->user->shop_id;
    $insertData['created_at'] = date('Y-m-d H:i:s');
    $insertData['status'] = 0;
    $insertData['created_by_name'] = $data->user->user_name;
    $insertData['created_by'] = $data->user->id;
    // 查询是否存在 如果存在 直接流转
    // 需要修改 抖音里面的状态
    $isTrue = $this->getOne(['order_num' => $insertData['order_num']]);
    if (!$isTrue) {
      DB::table('douyin_catch')->where(['id' => $data['ids']])->update(['is_sign' => 1]);
    }
    if ($isTrue) {
      DB::table('douyin_remark')->where(['order_num' => $insertData['order_num']])->update(['status' => 1]);
    }

    $res = $this->model->insert($insertData);
    if ($res) {
      return $this->returnMsg(200, '添加成功');
    }
    return $this->returnMsg(1, '添加失败');
  }
  //删除
  public function del($where)
  {
    $flag = true;
    $msg = '未知错误';
    $code = 1;
    if (empty($where)) {
      $flag = false;
      $msg = '删除条件不能为空';
      $code = 2;
    }
    if ($flag) {
      $res = $this->model->where($where)->get();
      if (!$res) {
        $flag = false;
        $msg = '未查询到数据';
        $code = 3;
      }
    }
    if ($flag) {
      $del = $this->model->where($where)->delete();
      if ($del) {
        $msg = '删除成功';
        $code = 200;
      }
    }
    return $this->returnMsg($code, $msg);
  }
  //修改
  public function edit($where, $data, $isMust = false)
  {
    if (empty($where)) {
      return $this->returnMsg(1, '条件不能为空');
    }
    if (empty($data)) {
      return $this->returnMsg(2, '修改参数不能为空');
    }
    //过滤字段
    $optionalFields = $this->model->getTableField();
    $field  = $this->getRealData($data, [], $optionalFields, []);
    $isTrue = $this->model->where($where)->first();
    if (!$isTrue) {
      return $this->returnMsg(100, '未查询到修改的数据');
    }
    //先查询是否存在
    $res = $this->model->where($where)->update($field);
    if ($isMust && $res) {
      if ($field['status'] == 2) {
        // $updateData = ['is_sign' => 0];
        DB::table('douyin_catch')->where(['dist_orderno' => $isTrue->order_num])->update(['is_sign' => 0]);
      }

      return $this->returnMsg(200, '修改成功');
    }
    if (!$isMust) {
      return $this->returnMsg(200, '修改成功');
    }
    return $this->returnMsg(3, '请重新修改');
  }
  //查看  存入缓存
  public function sel($where = [], $page = 1, $limit = 10)
  {
    $res = $this->search($this->model, $where, $page, $limit);
    return $res;
  }
  //查询 单条数据
  public function oneSel($where)
  {
    return $this->model->where($where)->first();
  }
  public function getOne($where, $field = ['*'])
  {
    return $this->model->where($where)->select($field)->orderBy('id', 'desc')->first();
  }
}
